<?php 
	include_once 'dataservice.php';
	include_once 'thuvienham.php';

	class lophoc_db 
	{
		public function Them($MaLopHoc, $MaKhoaHoc, $MaChuongTrinh, $MaThoiGianHoc, $PIN, $MaPhong)
		{
			$sql = "INSERT INTO `tbl_lophoc`(`MaLopHoc`, `MaKhoaHoc`, `MaChuongTrinh`, `MaThoiGianHoc`, `PIN`, `MaPhong`)
					VALUES ('$MaLopHoc', '$MaKhoaHoc', '$MaChuongTrinh', '$MaThoiGianHoc', '$PIN', '$MaPhong')";

			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}

		public function Xoa($MaLopHoc)
		{
			$sql = "DELETE FROM `tbl_lophoc` WHERE `MaLopHoc` = '$MaLopHoc'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}

		public function CapNhat($MaLopHoc, $MaKhoaHoc, $MaChuongTrinh, $MaThoiGianHoc, $PIN, $MaPhong)
		{
			$sql = "UPDATE `tbl_lophoc` SET ";
			$tmp = 0;
			if($MaKhoaHoc != "")
			{
				$tmp = 1;
				$sql .= " `MaKhoaHoc` = '$MaKhoaHoc' ";
			}
			if($MaChuongTrinh != "")
			{
				$t = ($tmp == 1) ? " , " : "" ;
				$tmp = 1;
				$sql .= $t . " `MaChuongTrinh` = '$MaChuongTrinh' ";
			}
			if($MaThoiGianHoc != "")
			{
				$t = ($tmp == 1) ? " , " : "" ;
				$tmp = 1;
				$sql .= $t . "  `MaThoiGianHoc` = '$MaThoiGianHoc' ";
			}
			if($PIN != "")
			{
				$t = ($tmp == 1) ? " , " : "" ;
				$tmp = 1;
				$sql .= $t . "  `PIN` = '$PIN' ";
			}
			if($MaPhong != "")
			{
				$t = ($tmp == 1) ? " , " : "" ;
				$tmp = 1;
				$sql .= $t . "  `MaPhong` = '$MaPhong' ";
			}
			$sql .= " WHERE `MaLopHoc` = '$MaLopHoc' ";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}

		public function LayDanhSach($MaLopHoc, $MaKhoaHoc, $MaChuongTrinh, $MaThoiGianHoc, $PIN, $MaPhong)
		{
			$sql = "SELECT * FROM `tbl_lophoc` WHERE 1  ";
			if($MaLopHoc != "")
				$sql .= " AND `MaLopHoc` = '$MaLopHoc' ";

			if($MaKhoaHoc != "")
				$sql .= " AND `MaKhoaHoc` = '$MaKhoaHoc' ";

			if($MaChuongTrinh != "")
				$sql .= " AND `MaChuongTrinh` = '$MaChuongTrinh' ";

			if($MaThoiGianHoc != "")
				$sql .= " AND `MaThoiGianHoc` = '$MaThoiGianHoc' ";

			if($PIN != "")
				$sql .= " AND `PIN` = '$PIN' ";

			if($MaPhong != "")
				$sql .= " AND `MaPhong` = '$MaPhong' ";
			$sql .= " ORDER BY MaLopHoc DESC ";
			
			$service = new dataservice();
			return $service->ExecuteQuery($sql);
		}
		
		public function LayDanhSachPaging($MaLopHoc, $MaKhoaHoc, $MaChuongTrinh, $MaThoiGianHoc, $PIN, $MaPhong, $Start, $Limit)
		{
			$sql = "SELECT * FROM `tbl_lophoc` WHERE 1  ";
			if($MaLopHoc != "")
				$sql .= " AND `MaLopHoc` = '$MaLopHoc' ";

			if($MaKhoaHoc != "")
				$sql .= " AND `MaKhoaHoc` = '$MaKhoaHoc' ";

			if($MaChuongTrinh != "")
				$sql .= " AND `MaChuongTrinh` = '$MaChuongTrinh' ";

			if($MaThoiGianHoc != "")
				$sql .= " AND `MaThoiGianHoc` = '$MaThoiGianHoc' ";

			if($PIN != "")
				$sql .= " AND `PIN` = '$PIN' ";

			if($MaPhong != "")
				$sql .= " AND `MaPhong` = '$MaPhong' ";
			$sql .= " ORDER BY MaLopHoc DESC LIMIT $Start, $Limit ";
			$service = new dataservice();
			return $service->ExecuteQuery($sql);
		}

	}

